'use strict';//联系我们和隐私政策
const Controller = require('egg').Controller;
class we_privacyController extends Controller {
    async index() { //模糊搜索跟分页查询
        const { ctx, app } = this;
        const { keyname, page = 1, number = 10 } = ctx.request.query
        const offset = (page - 1) * number
        let data, count
        if (keyname && page) {
            data = await app.mysql.query(`select id  we_title,we_start,we_customer_service,	we_business_contact,we_advertising_cooperation,	we_ending,privacy_title,privacy_content from we_privacy where id like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from we_privacy where id like '%${keyname}%'`)
        } else if (keyname) {
            data = await app.mysql.query(`select id  we_title,we_start,we_customer_service,	we_business_contact,we_advertising_cooperation,	we_ending,privacy_title,privacy_content from we_privacy where id ='%${keyname}%' or title  like '%${keyname}%' limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from title where id like '%${keyname}%' or id like '%${keyname}%'`)
        } else {
            data = await app.mysql.query(`select id  we_title,we_start,we_customer_service,	we_business_contact,we_advertising_cooperation,	we_ending,privacy_title,privacy_content from we_privacy limit ${offset},${number}`)
            count = await app.mysql.query(`select count(*) as count from we_privacy`)
        }
        ctx.body = {
            code: 0,
            msg: "查找成功",
            data,
            count: count
        }
    }

    async update() {
        const { ctx, app } = this;
        const { we_title, we_start, we_customer_service, we_business_contact, we_advertising_cooperation, we_ending, privacy_title, privacy_content } = ctx.request.body;
        const { id } = ctx.params;
        const row = {
            we_title,
            we_start,
            we_customer_service, we_business_contact, we_advertising_cooperation,
            we_ending,
            privacy_title,
            privacy_content

        }
        const options = {
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('we_privacy', row, options)
        if (we_title, we_start, we_customer_service, we_business_contact, we_advertising_cooperation, we_ending, privacy_title, privacy_content) {
            ctx.body = {
                code: 0,
                msg: "修改成功",
                data: result
            };
        } else {
            ctx.body = {
                code: 408,
                msg: "修改失败",
            };
        }
    }

   
}
module.exports = we_privacyController;
